<template>
	<!-- 武威监狱版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('public.materialCode')" />
		<!-- 表格数据 -->
		<zy-table :headList="headList" :dataList="dataList" @editConfirm="editConfirm" :operationList="operationList"
			border>
		</zy-table>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submit"
			@leftBtn="resetBtn" :leftDisabled="dataList.length>0?false:true"
			:rightDisabled="dataList.length>0?false:true">
		</bottomBtn>
		<!-- 选择按钮弹窗 -->
		<zy-popup ref="zyPopup" type='dialog' :title="$t('homepage.WarehouseManage.select')" @confirm="secondSubmit"
			:closeOnClickOverlay="false">
			<zy-forms :model="model" :rules="rules" labelWidth="100">
				<zy-form-item :label="$t('public.department')" required>
					<picker mode="multiSelector" @change="departChange" :value="departIndex" :range="departColumn">
						<view class="formItem" @click="departBtn">
							<input class="formInput" v-model="model.depart" disabled
								:placeholder="$t('public.pleaseSelect')+$t('public.department')" />
							<i class="iconfont icon-xiangxia1"></i>
						</view>
					</picker>
				</zy-form-item>
			</zy-forms>
		</zy-popup>
		<!-- 出库提交二次确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.outstorageSure')" type="dialog" @confirm="submitBtn">
			{{$t('homepage.AccessoryManagement.isSureOutstorage')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getStorehouseDetailByCode,
		queryDictByType,
		queryTreeList,
		saveStorehouseOut
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				//表格表头
				headList: [{
					name: this.$t('homepage.WarehouseManage.instorageCode'),
					key: 'barcode',
					width: 150
				}, {
					name: this.$t('homepage.WarehouseManage.materialCode'),
					key: 'materialCode',
				}, {
					name: this.$t('public.materialName'),
					key: 'materialName',
				}, {
					name: this.$t('homepage.WarehouseManage.unitInfo'),
					key: 'unitInfo',
				}, {
					name: this.$t('public.surplusNum'),
					key: 'surplusNum',
					sum: true
				}, {
					name: this.$t('public.deliveryNum'),
					key: 'num',
					edit: true,
					type: 'number',
					sum: true
				}],
				dataList: [], //表格数据
				//表格操作列
				operationList: [{
					kind: "button",
					type: "warn",
					handle: this.delBtn,
					label: this.$t('public.del')
				}],
				//弹窗表单数据
				model: {
					depart: '',
				},
				//form校验
				rules: {
					depart: {
						required: true,
						message: this.$t('public.pleaseSelect') + this.$t('public.department'),
						trigger: ['blur', 'change']
					}
				},
				departColumn: [], //部门下拉框数据
				departIndex: [0, 0],
				departParent: [],
				departChild: [],
				departChilds: [],
				departId: '',
			}
		},
		onLoad() {
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				getStorehouseDetailByCode({
					code: e,
				}).then((res) => {
					if (res.code == 0) {
						if (res.data.status == 1) {
							if (this.dataList.findIndex(item => item.barcode === e) !== -1) {
								uni.showToast({
									title: this.$t('public.searchRepeat'),
									icon: "none",
									duration: 3000
								})
							} else {
								if (res.data.num > res.data.surplusNum) {
									res.data.num = res.data.surplusNum
								}
								let data = JSON.stringify(res.data)
								this.dataList.push(JSON.parse(data))
							}
						} else {
							let message = ""
							if (res.data.status == 0) {
								message = '未入库'
							} else if (res.data.status == 2) {
								message = '已出库'
							} else if (res.data.status == 3) {
								message = '已撤销'
							}
							uni.showToast({
								title: "此编码" + message,
								icon: "none",
								duration: 3000
							})
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//编辑出库数量
			editConfirm(val) {
				if (Number(val.item.num) > Number(this.dataList[val.index].surplusNum)) {
					this.dataList[val.index].num = this.dataList[val.index].surplusNum
					uni.showToast({
						title: this.$t('public.max') + this.dataList[val.index]
							.surplusNum,
						icon: "none",
						duration: 3000
					})
				}
			},
			// 删除数据事件
			delBtn(item, index) {
				this.dataList.splice(index, 1);
			},
			//重置按钮
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/Outstorage/index"
				})
			},
			// 选择生产单事件
			selectHandle() {
				uni.navigateTo({
					url: "./list"
				})
			},
			//出库部门选择按钮事件
			submit() {
				this.$refs.zyPopup.open()
			},
			//部门按钮
			departBtn() {
				this.departColumn = []
				this.departParent = []
				this.departChild = []
				this.departChilds = []
				queryTreeList().then((res) => {
					//选择器中父列数据
					this.departParent = res.data.map((item) => item.title)
					//选择器中子列数据
					for (var i = 0; i < res.data.length; i++) {
						for (var j = 0; j < res.data[i].children.length; j++) {
							let obj = {}
							obj.label = res.data[i].children[j].title,
								obj.id = res.data[i].children[j].id
							this.departChild.push(res.data[i].children[j].title)
							this.departChilds.push(obj)
						}
					}
					this.departColumn.push(this.departParent, this.departChild)
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//部门弹框确认
			departChange(e) {
				this.departChilds.forEach((item, index) => {
					if (e.detail.value[1] == index) {
						this.departId = item.id
						this.model.depart = item.label
					}
				})
			},
			// 提交出库二次确认
			secondSubmit() {
				this.$refs.popup.open()
			},
			//确认出库
			submitBtn() {
				saveStorehouseOut({
					dataArray: JSON.stringify(this.dataList), //库存数组 （扫码获取的数据，多个逗号隔开）
					applyDept: this.model.depart,
				}).then((res) => {
					if (res.code == 0) {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						this.resetBtn()
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	.formItem {
		width: calc(100% - 40rpx);
		padding: 20rpx;
		display: flex;
		border: 1px solid #e9e9e9;
		border-radius: 10rpx;
		background-color: #f5f5f5;
	}

	.formInput {
		margin: auto 0;
		width: 100%;
		border: none;
		outline: none;
		font-size: 32rpx;
		z-index: 999999;
	}

	.iconfont {
		margin: auto 0 auto 10rpx;
		font-size: 28rpx;
		color: #999999;
	}
</style>